package com.example.idie.controller;

import com.example.idie.bean.User;
import com.example.idie.service.UserLoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @Resource
    UserLoginService userLoginService;

    @CrossOrigin
    @PostMapping(value = "/user/login")
    @ResponseBody
    public Map login(@RequestParam("username") String username,
                     @RequestParam("password") String password,
                     @RequestParam("usertype") Integer usertype) {
        System.out.println(username + " " + password);
        System.out.println(usertype);
//        System.out.println(userLoginService.validateLogin(username, password));
        return userLoginService.validateLogin(username, password, usertype);
//
//        HashMap<String, Object> response = new HashMap<>();
//        response.put("code", 1000);
//        response.put("msg", "登录成功");
//        response.put("token", username + "-login");
//        return response;
    }

    @CrossOrigin
    @GetMapping(value = "/user/info")
    @ResponseBody
    public Map info(@RequestParam("token") String token) {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        String userName = token.split("-")[0];
        User user = userLoginService.findUserByName(userName);
        String userRole = user.getUserRole();
        data.put("roles", userRole);
        data.put("introduction", "I am a ".concat(userRole));
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        data.put("name", user.getUsername());

        response.put("code", 1000);
        response.put("data", data);
        //return userLoginService.getInfo(userId);
        return response;
    }

    @CrossOrigin
    @PostMapping(value = "/user/logout")
    @ResponseBody
    public Map logout() {
        HashMap<String, Object> response = new HashMap<>();
        response.put("code", 1000);

        response.put("data", "注销成功");
        return response;
    }

    @CrossOrigin
    @PostMapping(value = "/user/changePassword")
    @ResponseBody
    public Map changePassword(@RequestParam("username") String username,
                              @RequestParam("oldPassword") String oldPassword,
                              @RequestParam("newPassword") String newPassword) {
        return userLoginService.changePassword(username, oldPassword, newPassword);
    }

    @CrossOrigin
    @PostMapping(value = "/user/register")
    @ResponseBody
    public Map register(@RequestParam("newUsername") String username,
                        @RequestParam("newPassword") String password,
                        @RequestParam("usertype") Integer usertype){

        return userLoginService.register(username, password, usertype);
    }


    @CrossOrigin
    @GetMapping(value = "/transaction/list")
    @ResponseBody
    public Map transactionList() {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        ArrayList<Map<String, Object>> items = new ArrayList<>();


        HashMap<String, Object> item = new HashMap<>();
        item.put("order_no", "ba0Dc771-7d8d-9C2D-BFF1-dDCcc8313C00");
        item.put("timestamp", 500673251);
        item.put("username", "Robert White");
        item.put("price", 11004);
        item.put("status", "pending");

        HashMap<String, Object> item1 = new HashMap<>();
        item1.put("order_no", "47EE276d-cCFc-f9e2-23cC-DC29DEfcb15A");
        item1.put("timestamp", 500671451);
        item1.put("username", "William Thompson");
        item1.put("price", 14124);
        item1.put("status", "pending");

        HashMap<String, Object> item2 = new HashMap<>();
        item2.put("order_no", "D9F99Abc-2210-baaC-fAbb-814C6557AbB9");
        item2.put("timestamp", 501671251);
        item2.put("username", "Deborah Thompson");
        item2.put("price", 11257);
        item2.put("status", "success");


        items.add(item);
        items.add(item1);
        items.add(item2);

        response.put("code", 1000);
        data.put("total", 3);
        data.put("items", items);
        response.put("data", data);
        return response;
    }

}
